<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Rationale</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../boost_typeerasure.html" title="Chapter 38. Boost.TypeErasure">
<link rel="prev" href="../doxygen/typeid__of_8hpp_1ab78e80ef4c5cde5f1b978093811c67ad.html" title="Function typeid_of">
<link rel="next" href="future.html" title="Future Work">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../doxygen/typeid__of_8hpp_1ab78e80ef4c5cde5f1b978093811c67ad.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeerasure.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="future.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_typeerasure.rationale"></a><a class="link" href="rationale.html" title="Rationale">Rationale</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="rationale.html#boost_typeerasure.rationale.why_do_i_have_to_specify_the_presence_of_a_destructor_explicitly_">Why
      do I have to specify the presence of a destructor explicitly?</a></span></dt>
<dt><span class="section"><a href="rationale.html#boost_typeerasure.rationale.why_non_member_functions_">Why
      non-member functions?</a></span></dt>
<dt><span class="section"><a href="rationale.html#boost_typeerasure.rationale.placeholder">Why are the
      placeholders called <code class="computeroutput"><span class="identifier">_a</span></code>, <code class="computeroutput"><span class="identifier">_b</span></code> and not <code class="computeroutput"><span class="identifier">_1</span></code>
      <code class="computeroutput"><span class="identifier">_2</span></code></a></span></dt>
<dt><span class="section"><a href="rationale.html#boost_typeerasure.rationale.ref">Why not use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span></code>
      for references?</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeerasure.rationale.why_do_i_have_to_specify_the_presence_of_a_destructor_explicitly_"></a><a class="link" href="rationale.html#boost_typeerasure.rationale.why_do_i_have_to_specify_the_presence_of_a_destructor_explicitly_" title="Why do I have to specify the presence of a destructor explicitly?">Why
      do I have to specify the presence of a destructor explicitly?</a>
</h3></div></div></div>
<p>
        When using references the destructor isn't needed. By not assuming it implicitly,
        we allow capturing types with private or protected destructors by reference.
        For the sake of consistency, it must be specified when capturing by value
        as well.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeerasure.rationale.why_non_member_functions_"></a><a class="link" href="rationale.html#boost_typeerasure.rationale.why_non_member_functions_" title="Why non-member functions?">Why
      non-member functions?</a>
</h3></div></div></div>
<p>
        The members of <code class="computeroutput"><a class="link" href="../doxygen/classboost_1_1type__erasure_1_1any.html" title="Class template any">any</a></code>
        can be customized. By using free functions, we guarantee that we don't interfere
        with anything that a user might want.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeerasure.rationale.placeholder"></a><a class="link" href="rationale.html#boost_typeerasure.rationale.placeholder" title="Why are the placeholders called _a, _b and not _1 _2">Why are the
      placeholders called <code class="computeroutput"><span class="identifier">_a</span></code>, <code class="computeroutput"><span class="identifier">_b</span></code> and not <code class="computeroutput"><span class="identifier">_1</span></code>
      <code class="computeroutput"><span class="identifier">_2</span></code></a>
</h3></div></div></div>
<p>
        An earlier version of the library used the names <code class="computeroutput"><span class="identifier">_1</span></code>,
        <code class="computeroutput"><span class="identifier">_2</span></code>, etc. instead of <code class="computeroutput"><span class="identifier">_a</span></code>, <code class="computeroutput"><span class="identifier">_b</span></code>,
        etc. This caused a certain amount of confusion because the numbered placeholders
        are already used with a somewhat different meaning by several other libraries
        including Boost/Std Bind, Boost.Phoenix, and Boost.MPL. I eventually decided
        that since the placeholders represented named parameters instead of positional
        parameters, letters were more appropriate than numbers.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeerasure.rationale.ref"></a><a class="link" href="rationale.html#boost_typeerasure.rationale.ref" title="Why not use boost::ref for references?">Why not use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span></code>
      for references?</a>
</h3></div></div></div>
<p>
        Boost.Function allows you to use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span></code> to
        store a reference to a function object. However, in the general case treating
        references and values in the same way causes inconsistent behavior that is
        difficult to reason about. If Boost.TypeErasure handled references like this,
        then, when you copy an <code class="computeroutput"><a class="link" href="../doxygen/classboost_1_1type__erasure_1_1any.html" title="Class template any">any</a></code>,
        you would have no idea whether the new object is a real copy or just a new
        reference to the same underlying object. Boost.Function can get away with
        it, because it doesn't expose any mutating operations on the stored function
        object.
      </p>
<p>
        Another method that has been proposed is only to keep a reference the first
        time.
      </p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
<span class="identifier">any</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">ref</span><span class="special">(</span><span class="identifier">i</span><span class="special">);</span>
<span class="identifier">any</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">x</span><span class="special">;</span> <span class="comment">// makes a copy</span>
</pre>
<p>
        Unfortunately, this doesn't handle all use cases, as there is no reliable
        way to return such a reference from a function. In addition it adds overhead
        whether it's needed or not, as we would have to add a flag to any to keep
        track of whether or not it is storing a reference. (The alternate method
        of storing this in the "<code class="computeroutput"><span class="identifier">clone</span></code>"
        method in the vtable is impossibly complex to implement given the decoupled
        vtables that Boost.TypeErasure uses and it still adds overhead.).
      </p>
</div>
</div>
<div class="copyright-footer">Copyright © 2011-2013 Steven Watanabe<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../doxygen/typeid__of_8hpp_1ab78e80ef4c5cde5f1b978093811c67ad.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeerasure.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="future.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
